package com.itheima.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itheima.dao.WebConfigDao;
import com.itheima.pojo.File;
import com.itheima.pojo.WebConfig;
import com.itheima.service.WebConfigService;
import com.itheima.utils.DateUtils;
import com.itheima.utils.JacksonUtil;
import com.itheima.vo.WebConfigVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @author lihu
 * @date 2020/7/8 16:53
 */
@Service

public class WebConfigServiceImpl implements WebConfigService {

    @Autowired
    private WebConfigDao webConfigDao;


    /**
     * 获取网站配置信息
     * @param webConfig
     * @param files
     * @return
     */
    @Override
    public WebConfigVO getWebConfigList(WebConfig webConfig, String files) {
        WebConfigVO webConfigVO = getWebConfigVO(webConfig,files);
        return webConfigVO;

    }

    @Override
    public void edit(WebConfigVO webConfigVO) {
        WebConfig webConfig = new WebConfig();
        webConfig.setLogo(webConfigVO.getLogo());
        webConfig.setAliPay(webConfigVO.getAliPay());
        webConfig.setAuthor(webConfigVO.getAuthor());
        webConfig.setEmail(webConfigVO.getEmail());
        webConfig.setGitee(webConfigVO.getGitee());
        webConfig.setGithub(webConfigVO.getGithub());
        webConfig.setKeyword(webConfigVO.getKeyword());
        webConfig.setLoginTypeList(webConfigVO.getLoginTypeList());
        webConfig.setName(webConfigVO.getName());
        webConfig.setQqGroup(webConfigVO.getQqGroup());
        webConfig.setRecordNum(webConfigVO.getRecordNum());
        webConfig.setShowList(webConfigVO.getShowList());
        webConfig.setStartComment(webConfigVO.getStartComment());
        webConfig.setStatus(webConfigVO.getStatus());
        webConfig.setTitle(webConfigVO.getTitle());
        webConfig.setWeChat(webConfigVO.getWeChat());
        webConfig.setWeixinPay(webConfigVO.getWeixinPay());
        webConfig.setUpdateTime(DateUtils.getNowTime());
        webConfig.setUid(webConfigVO.getUid());
       webConfigDao.updateByPrimaryKeySelective(webConfig);
    }

    @Override
    public List<WebConfig> getWebConfig() {
        List<WebConfig> list = webConfigDao.selectAll();
        return list;
    }

    /**
     * 封装WebConfigVO
     * @param webConfig
     * @param files  图片文件信息
     * @return
     */
    public WebConfigVO getWebConfigVO(WebConfig webConfig, String files) {
        WebConfigVO webConfigVO = new WebConfigVO();
        webConfigVO.setUid(webConfig.getUid());
        webConfigVO.setLogo(webConfig.getLogo());
        webConfigVO.setName(webConfig.getName());
        webConfigVO.setKeyword(webConfig.getKeyword());
        webConfigVO.setAuthor(webConfig.getAuthor());
        webConfigVO.setRecordNum(webConfig.getRecordNum());
        webConfigVO.setAliPay(webConfig.getAliPay());
        webConfigVO.setWeixinPay(webConfig.getWeixinPay());
        webConfigVO.setStartComment(webConfig.getStartComment());
        webConfigVO.setEmail(webConfig.getEmail());
        webConfigVO.setQqNumber(webConfig.getQqNumber());
        webConfigVO.setQqGroup(webConfig.getQqGroup());
        webConfigVO.setGithub(webConfig.getGithub());
        webConfigVO.setGitee(webConfig.getGitee());
        webConfigVO.setWeChat(webConfig.getWeChat());
        webConfigVO.setStatus(webConfig.getStatus());

        //图片文件信息json转换成map,去除data里的数据
        Map<String, Object> map = null;
        try {
            map = JacksonUtil.json2map(files);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Object data = map.get("data");
        //data里的json转换成String
        String str = JSONObject.toJSONString(data);
        //String转换成list<File>集合取出对应的数据
        List<File> filesList = JSONArray.parseArray(str, File.class);
        webConfigVO.setAliPayPhoto(filesList.get(1).getQiNiuUrl());
        webConfigVO.setWeixinPayPhoto(filesList.get(2).getQiNiuUrl());
        webConfigVO.setShowList(webConfig.getShowList());
        webConfigVO.setLoginTypeList(webConfig.getLoginTypeList());

        List<String> photoList = new ArrayList<>();
        photoList.add(webConfig.getLogo());
        webConfigVO.setPhotoList(photoList);

        return webConfigVO;
    }

}
